<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-08-15
 * Time: 20:56
 */
namespace app\wxapi\controller;

use app\common\controller\Wxapibase;
use app\common\model\User as cxModel;
use app\common\model\UserMoney;

class User extends Wxapibase {

    public function index(){

        $webs = array(
            'title' => '我的订单',
        );
        $this->assign([
            'webs' => $webs
        ]);
        return view();
    }
    // 我的订单
    public function dingdan(){
        $getdata = request()->param();
        if(empty($getdata['f']) || $getdata['f'] == 'all'){
            $map = null;
        }elseif($getdata['f'] == 'wf'){
            $map['playstatus'] = array('in','0,4');
        }elseif($getdata['f'] == '3' || $getdata['f'] == '6'){
            $map['playstatus'] = array('in','3,6');
        }else{
            $map['playstatus'] = $getdata['f'];
        }
        $ormodel = new \app\common\model\Order();
        $orlist = $ormodel->where('uid',$this->cxbsuser['uid'])->where($map)->where('mid','100')->order('addtime desc')->paginate('20')->each(function ($item, $key){
            $item->conf = unserialize($item->conf);
            $item->comment = model('Comment')->where('oid',$item->oid)->where('uid',$this->cxbsuser['uid'])->count();
        });
        $webs = array(
            'title' => '我的订单',
        );
        $this->assign([
            'orlist' => $orlist,
            'thistime' => time(),
            'webs' => $webs
        ]);
        return view();
    }
    // 我的订单
    public function tuango(){
        $getdata = request()->param();
        if(empty($getdata['f']) || $getdata['f'] == 'all'){
            $map = null;
        }elseif($getdata['f'] == 'wf'){
            $map['playstatus'] = array('in','0,4');
        }elseif($getdata['f'] == '3' || $getdata['f'] == '6'){
            $map['playstatus'] = array('in','3,6');
        }else{
            $map['playstatus'] = $getdata['f'];
        }
        $ormodel = new \app\common\model\Order();
        $orlist = $ormodel->where('uid',$this->cxbsuser['uid'])->where($map)->where('mid','101')->order('addtime desc')->paginate('20')->each(function ($item, $key){
            $item->tnum = model('Order')->where('foid',$item->oid)->where('playstatus','<>','0')->count();
            $item->conf = unserialize($item->conf);
            $item->comment = model('Comment')->where('oid',$item->oid)->where('uid',$this->cxbsuser['uid'])->count();
        });
        $webs = array(
            'title' => '我的团购',
        );
        $this->assign([
            'orlist' => $orlist,
            'thistime' => time(),
            'webs' => $webs
        ]);
        return view();
    }
    //  我的推广
    public function money(){
        $monty = new UserMoney();
        $montylist = $monty->where('uid',$this->cxbsuser['uid'])->order('addtime desc')->paginate(30);
        $montys = $monty->where('uid',$this->cxbsuser['uid'])->order('addtime desc')->find();
        $webs = array(
            'title' => '我的推广',
        );
        $this->assign([
            'montylist' => $montylist,
            'montys' => $montys,
            'webs' => $webs
        ]);
        return view();
    }
    //  修改个人资料
    public function edit_user(){
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            if($data['uid'] != $this->cxbsuser['uid']){
                $this->error("非法访问！");
            }
            $cxmodel = new cxModel();
            if($cxmodel->edit_user($data)){
                $this->success("个人资料修改成功！");
            }else{
                $this->error("个人资料修改失败！");
            }
        }
        $wxjsapi = $this->get_wxjsapi($this->webdb['www_url'].url('User/edit_user'));
        $webs = array(
            'title' => '我的资料',
        );
        $this->assign([
            'webs' => $webs,
            'wxjsapi' => $wxjsapi,
        ]);
        return view();
    }
    public function erma(){
        $getdata = request()->param();
        if(empty($getdata['aid'])){
            $this->error("非法访问");
        }
        $shop = controller('Order')->get_shop($getdata['aid']);
        $urls = $this->webdb['www_url'].url('Article/index',array('aid'=>$shop['aid'],'tuijian'=>$this->cxbsuser['uid']));
        $urls = $this->get_qrcode($urls);
        $upmodel = new \app\common\controller\Upload();
        if(is_file($this->webdb['updir'].'/tuiguang/'.$this->cxbsuser['uid'].'_'.$shop['aid'].'_tuiguang.jpg')){
            $timgtime = filectime($this->webdb['updir'].'/tuiguang/'.$this->cxbsuser['uid'].'_'.$shop['aid'].'_tuiguang.jpg');
            $img = $this->webdb['updir'].'/tuiguang/'.$this->cxbsuser['uid'].'_'.$shop['aid'].'_tuiguang.jpg';
        }else{
            if($shop['mid'] == '100'){
                switch ($this->cxbsuser['group_id']){
                    case '6':
                        $shop['money'] = '￥ '.number_format($shop['yinjia'],'2');
                        break;
                    case '7':
                        $shop['money'] = '￥ '.number_format($shop['jinjia'],'2');
                        break;
                    case '8':
                        $shop['money'] = '￥ '.number_format($shop['pifajia'],'2');
                        break;
                    default:
                        $shop['money'] = '￥ '.number_format($shop['shichuangjia'],'2');
                        break;
                }
            }else{
                $shop['money'] = '￥ '.number_format($shop['pinmoney'],'2');
                if(empty($shop['picurl'])){
                    $shop['picurl'] = $shop['pinimg'];
                }elseif(empty($shop['picurl']) && empty($shop['pinimg'])){
                    $shop['picurl'] = $shop['paid']['0']['picurl'];
                }
            }
            //  打包需要的参数
            $newshop = array(
                'aid' => $shop['aid'],
                'title' => $shop['title'],
                'picurl' => $shop['picurl'],
                'money' => $shop['money'],
                'text' => '长按识别二维码访问',
                'webtitle' => $this->webdb['webname'],
            );
            $erweima = "http://qr.liantu.com/api.php?text=".$urls;
            $img = $upmodel->tuiguang($this->cxbsuser['uid'],$newshop,$erweima,$getdata);
        }
        $img = request()->domain().'/'.$img;
        return $img;
    }
    // 添加收藏
    public function shoucang(){
        if(request()->isPost()){
            $cxmodel = new cxModel();
            $data = input('post.');
            $data = datatrim($data);
            $data['uid'] = $this->cxbsuser['uid'];
            if($cxmodel->shoucang($data)){
                $this->cxbsuser = session('userdb');
                $this->success("收藏成功！");
            }else{
                $this->error("收藏失败！");
            }
        }
        return false;
    }

}